/*
Filename: UCAdminDeleteMgr.java
Precondition: Adminster login success.
Post-condition: Admin delete manager success.
1. Adminster input manager ID.
2. Adminster click on delete.
3. System validUser() valid the ID.
4. System delete the manager ingormation.
 */

package ucm;

/**
 *
 * @author Mark
 */
import model.UsersModel;
import model.DatabaseModel;
import view.MgrInformation;

import javax.swing.*;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
public class UCAdminDeleteMgr implements UCController {

    private UsersModel user;

    public UCAdminDeleteMgr() {
        user = new UsersModel("", "", "", "", "");
    }

    public UCAdminDeleteMgr(UsersModel user) {
        // System retrieves username and password
        this.user = user;
    }

    public UCAdminDeleteMgr(MgrInformation MgrInformation) {
        // System retrieves username and password
        this.user = MgrInformation.getUserModel();
    }

    // STEP 1 - User Selects Delete
    public boolean run() {
        boolean deleteSuccessful = false;
        // STEP 2 - System retrieves username and password
        // This step is integrated into the constructor or
        // may be specified by using run(UserModel)
        // STEP 3 - System validates the username and password
        if (validUser()) {
            //Step 4 - System deletes user from the database
            if (deleteUser()) {
                //Step 5 - System displays 'user deleted' message
                JOptionPane.showMessageDialog(new JFrame(),
                        "Manager Deleted Successfully",
                        "Success",
                        JOptionPane.INFORMATION_MESSAGE);
                deleteSuccessful = true;
                new ucm.UCDisplayMgrInformation();
            } else {
                JOptionPane.showMessageDialog(new JFrame(),
                        "Deletion failed for unknown reason",
                        "Error Message",
                        JOptionPane.ERROR_MESSAGE);
                new ucm.UCDisplayMgrInformation();
            }
        } else {
            // STEP 2a - System displays error message
            JOptionPane.showMessageDialog(new JFrame(),
                    "Invalid ID. Try again.",
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
            new ucm.UCDisplayMgrInformation();
        }
        return deleteSuccessful;
    }

    // STEP 1 - User Selects Delete
    public void run(UsersModel user) {
        // STEP 2 - System retrieves username and password
        this.user = user;
        // STEP 3 onwards
        run();
    }

    private boolean validUser() {
        boolean correct = false;
        String username = user.getUsername();

        Connection con = DatabaseModel.getInstance().getConnection();
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE,
                    ResultSet.CONCUR_UPDATABLE);

            ResultSet res = stmt.executeQuery("SELECT * FROM managers WHERE " +
                    "mid = '" + username + "'");

            correct = res.first();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return correct;
    }

    private boolean deleteUser() {
        boolean delete = false;
        String username = user.getUsername();
        Connection con = DatabaseModel.getInstance().getConnection();
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE,
                    ResultSet.CONCUR_UPDATABLE);

            stmt.executeUpdate("DELETE FROM managers WHERE mid = '"
                    + username + "'");

            ResultSet res = stmt.executeQuery("SELECT * FROM managers " +
                    "WHERE mid = '" + username + "'");

            delete = !(res.first());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return delete;
    }
}



